//Copyright (C) 2000 Microsoft Corporation.  All rights reserved.

//This source code is intended only as a supplement to Microsoft
//Development Tools and/or on-line documentation.  See these other
//materials for detailed information regarding Microsoft code samples.

//THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//PARTICULAR PURPOSE.

namespace Slaks.TextParsing
{
using System;
using System.IO ;
using System.Text;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Threading;

using Slaks.TextParsing.Encoding;

public enum ParsingMode { DIRECTORY, FILE };

public class MDIView : System.Windows.Forms.Form
{

  private object[] ENCODING = {
                                new Enc(System.Text.Encoding.UTF8,"UTF8"),
                                new Enc(System.Text.Encoding.GetEncoding("windows-1256"),"Windows-1256"),
                                new Enc(System.Text.Encoding.GetEncoding("windows-1255"),"Windows-1255"),
                                new Enc(System.Text.Encoding.GetEncoding("windows-1251"),"Windows-1251"),
                                };

  /// <summary> 
  ///    Required designer variable
  /// </summary>
  private System.ComponentModel.Container components;
	private MDIDoc myDoc;
  private System.Windows.Forms.GroupBox m_fileParsingGB;
  private System.Windows.Forms.ListBox m_fileParsingLB;
  private System.Windows.Forms.Button m_cancelBtn;
  private System.Windows.Forms.Button m_proceedBtn;
  private System.Windows.Forms.GroupBox m_parsingTypeGB;
  private System.Windows.Forms.RadioButton m_fileParsingDirectoryRB;
  private System.Windows.Forms.RadioButton m_fileParsingRB;
  private System.Windows.Forms.ComboBox m_encodingCombo;
  private System.Windows.Forms.GroupBox m_outputDirectoryGB;
  private System.Windows.Forms.TextBox m_outputDirectoryTB;
  private System.Windows.Forms.Button m_outputDirectoryBtn;
  private System.Windows.Forms.GroupBox m_encodingGB;
  private System.Windows.Forms.ListBox m_messagesLB;
  private System.Windows.Forms.ContextMenu m_contextMenu;
  private System.Windows.Forms.MenuItem m_addContext;
	private Form1 mainWin;
  private System.Windows.Forms.MenuItem m_deleteContext;
  private System.Windows.Forms.MenuItem m_clearAllContext;
  private ParsingMode m_parsingMode;
  private System.Windows.Forms.OpenFileDialog m_addFilesFD;
  private IList m_filesToParseList = null;
  private System.Windows.Forms.MainMenu m_mainMenuMN;
  private System.Windows.Forms.MenuItem m_countFilexContext;
  private System.Windows.Forms.MenuItem m_configureMI;
  private System.Windows.Forms.MenuItem m_textFilteringMI;
  private Progress m_progress = null;
	private System.Windows.Forms.Panel panel1;
	private System.Windows.Forms.Splitter splitter1;
  private TextFiltering m_textFiltering = null;

  public MDIView(MDIDoc doc,Form1 parent)
  {
		//
    // Required for Win Form Designer support
    //
    InitializeComponent();
    
		this.myDoc  = doc;
		this.MdiParent = parent; //Make this view Mdi child of the main window
		mainWin = parent;	
    
    Init();
  }

    /// <summary>
    ///    Clean up any resources being used
    /// </summary>
	protected override void Dispose( bool disposing )
	{
		if( disposing )
		{
			if (components != null) 
			{
				components.Dispose();
			}
		}
		base.Dispose( disposing );
	}

    /// <summary>
    ///    Required method for Designer support - do not modify
    ///    the contents of this method with the code editor
    /// </summary>
  private void InitializeComponent()
	{
		this.m_fileParsingGB = new System.Windows.Forms.GroupBox();
		this.panel1 = new System.Windows.Forms.Panel();
		this.m_messagesLB = new System.Windows.Forms.ListBox();
		this.splitter1 = new System.Windows.Forms.Splitter();
		this.m_fileParsingLB = new System.Windows.Forms.ListBox();
		this.m_contextMenu = new System.Windows.Forms.ContextMenu();
		this.m_addContext = new System.Windows.Forms.MenuItem();
		this.m_deleteContext = new System.Windows.Forms.MenuItem();
		this.m_clearAllContext = new System.Windows.Forms.MenuItem();
		this.m_countFilexContext = new System.Windows.Forms.MenuItem();
		this.m_outputDirectoryGB = new System.Windows.Forms.GroupBox();
		this.m_outputDirectoryBtn = new System.Windows.Forms.Button();
		this.m_outputDirectoryTB = new System.Windows.Forms.TextBox();
		this.m_parsingTypeGB = new System.Windows.Forms.GroupBox();
		this.m_fileParsingRB = new System.Windows.Forms.RadioButton();
		this.m_fileParsingDirectoryRB = new System.Windows.Forms.RadioButton();
		this.m_proceedBtn = new System.Windows.Forms.Button();
		this.m_cancelBtn = new System.Windows.Forms.Button();
		this.m_encodingGB = new System.Windows.Forms.GroupBox();
		this.m_encodingCombo = new System.Windows.Forms.ComboBox();
		this.m_addFilesFD = new System.Windows.Forms.OpenFileDialog();
		this.m_mainMenuMN = new System.Windows.Forms.MainMenu();
		this.m_configureMI = new System.Windows.Forms.MenuItem();
		this.m_textFilteringMI = new System.Windows.Forms.MenuItem();
		this.m_fileParsingGB.SuspendLayout();
		this.panel1.SuspendLayout();
		this.m_outputDirectoryGB.SuspendLayout();
		this.m_parsingTypeGB.SuspendLayout();
		this.m_encodingGB.SuspendLayout();
		this.SuspendLayout();
		// 
		// m_fileParsingGB
		// 
		this.m_fileParsingGB.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
			| System.Windows.Forms.AnchorStyles.Left) 
			| System.Windows.Forms.AnchorStyles.Right)));
		this.m_fileParsingGB.Controls.Add(this.panel1);
		this.m_fileParsingGB.Controls.Add(this.m_outputDirectoryGB);
		this.m_fileParsingGB.Controls.Add(this.m_parsingTypeGB);
		this.m_fileParsingGB.Controls.Add(this.m_proceedBtn);
		this.m_fileParsingGB.Controls.Add(this.m_cancelBtn);
		this.m_fileParsingGB.Controls.Add(this.m_encodingGB);
		this.m_fileParsingGB.Location = new System.Drawing.Point(8, 8);
		this.m_fileParsingGB.Name = "m_fileParsingGB";
		this.m_fileParsingGB.Size = new System.Drawing.Size(664, 576);
		this.m_fileParsingGB.TabIndex = 0;
		this.m_fileParsingGB.TabStop = false;
		this.m_fileParsingGB.Text = "Files";
		// 
		// panel1
		// 
		this.panel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
			| System.Windows.Forms.AnchorStyles.Left) 
			| System.Windows.Forms.AnchorStyles.Right)));
		this.panel1.Controls.Add(this.m_messagesLB);
		this.panel1.Controls.Add(this.splitter1);
		this.panel1.Controls.Add(this.m_fileParsingLB);
		this.panel1.Location = new System.Drawing.Point(8, 168);
		this.panel1.Name = "panel1";
		this.panel1.Size = new System.Drawing.Size(648, 368);
		this.panel1.TabIndex = 9;
		// 
		// m_messagesLB
		// 
		this.m_messagesLB.BorderStyle = System.Windows.Forms.BorderStyle.None;
		this.m_messagesLB.Dock = System.Windows.Forms.DockStyle.Fill;
		this.m_messagesLB.HorizontalExtent = 6000;
		this.m_messagesLB.HorizontalScrollbar = true;
		this.m_messagesLB.Location = new System.Drawing.Point(0, 276);
		this.m_messagesLB.Name = "m_messagesLB";
		this.m_messagesLB.Size = new System.Drawing.Size(648, 91);
		this.m_messagesLB.TabIndex = 8;
		// 
		// splitter1
		// 
		this.splitter1.Dock = System.Windows.Forms.DockStyle.Top;
		this.splitter1.Location = new System.Drawing.Point(0, 273);
		this.splitter1.Name = "splitter1";
		this.splitter1.Size = new System.Drawing.Size(648, 3);
		this.splitter1.TabIndex = 9;
		this.splitter1.TabStop = false;
		// 
		// m_fileParsingLB
		// 
		this.m_fileParsingLB.BorderStyle = System.Windows.Forms.BorderStyle.None;
		this.m_fileParsingLB.ContextMenu = this.m_contextMenu;
		this.m_fileParsingLB.Dock = System.Windows.Forms.DockStyle.Top;
		this.m_fileParsingLB.Location = new System.Drawing.Point(0, 0);
		this.m_fileParsingLB.Name = "m_fileParsingLB";
		this.m_fileParsingLB.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
		this.m_fileParsingLB.Size = new System.Drawing.Size(648, 273);
		this.m_fileParsingLB.TabIndex = 0;
		// 
		// m_contextMenu
		// 
		this.m_contextMenu.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																																								this.m_addContext,
																																								this.m_deleteContext,
																																								this.m_clearAllContext,
																																								this.m_countFilexContext});
		this.m_contextMenu.Popup += new System.EventHandler(this.m_contextMenu_Popup);
		// 
		// m_addContext
		// 
		this.m_addContext.Index = 0;
		this.m_addContext.Text = "Add";
		this.m_addContext.Popup += new System.EventHandler(this.m_addContext_Popup);
		this.m_addContext.Click += new System.EventHandler(this.m_addContext_Click);
		// 
		// m_deleteContext
		// 
		this.m_deleteContext.Index = 1;
		this.m_deleteContext.Text = "Delete";
		this.m_deleteContext.Click += new System.EventHandler(this.m_deleteContext_Click);
		// 
		// m_clearAllContext
		// 
		this.m_clearAllContext.Index = 2;
		this.m_clearAllContext.Text = "Clear All";
		this.m_clearAllContext.Click += new System.EventHandler(this.m_clearAllContext_Click);
		// 
		// m_countFilexContext
		// 
		this.m_countFilexContext.Index = 3;
		this.m_countFilexContext.Text = "Count files";
		this.m_countFilexContext.Click += new System.EventHandler(this.menuItem1_Click);
		// 
		// m_outputDirectoryGB
		// 
		this.m_outputDirectoryGB.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) 
			| System.Windows.Forms.AnchorStyles.Right)));
		this.m_outputDirectoryGB.Controls.Add(this.m_outputDirectoryBtn);
		this.m_outputDirectoryGB.Controls.Add(this.m_outputDirectoryTB);
		this.m_outputDirectoryGB.Location = new System.Drawing.Point(8, 112);
		this.m_outputDirectoryGB.Name = "m_outputDirectoryGB";
		this.m_outputDirectoryGB.Size = new System.Drawing.Size(648, 48);
		this.m_outputDirectoryGB.TabIndex = 6;
		this.m_outputDirectoryGB.TabStop = false;
		this.m_outputDirectoryGB.Text = "Output directory";
		// 
		// m_outputDirectoryBtn
		// 
		this.m_outputDirectoryBtn.Anchor = System.Windows.Forms.AnchorStyles.Right;
		this.m_outputDirectoryBtn.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
		this.m_outputDirectoryBtn.Location = new System.Drawing.Point(616, 16);
		this.m_outputDirectoryBtn.Name = "m_outputDirectoryBtn";
		this.m_outputDirectoryBtn.Size = new System.Drawing.Size(24, 24);
		this.m_outputDirectoryBtn.TabIndex = 1;
		this.m_outputDirectoryBtn.Text = "...";
		this.m_outputDirectoryBtn.Click += new System.EventHandler(this.m_outputDirectoryBtn_Click);
		// 
		// m_outputDirectoryTB
		// 
		this.m_outputDirectoryTB.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Left | System.Windows.Forms.AnchorStyles.Right)));
		this.m_outputDirectoryTB.Location = new System.Drawing.Point(8, 16);
		this.m_outputDirectoryTB.Name = "m_outputDirectoryTB";
		this.m_outputDirectoryTB.Size = new System.Drawing.Size(608, 20);
		this.m_outputDirectoryTB.TabIndex = 0;
		this.m_outputDirectoryTB.Text = "";
		// 
		// m_parsingTypeGB
		// 
		this.m_parsingTypeGB.Controls.Add(this.m_fileParsingRB);
		this.m_parsingTypeGB.Controls.Add(this.m_fileParsingDirectoryRB);
		this.m_parsingTypeGB.Location = new System.Drawing.Point(8, 16);
		this.m_parsingTypeGB.Name = "m_parsingTypeGB";
		this.m_parsingTypeGB.Size = new System.Drawing.Size(128, 88);
		this.m_parsingTypeGB.TabIndex = 3;
		this.m_parsingTypeGB.TabStop = false;
		this.m_parsingTypeGB.Text = "Parsing type";
		// 
		// m_fileParsingRB
		// 
		this.m_fileParsingRB.Checked = true;
		this.m_fileParsingRB.Location = new System.Drawing.Point(8, 16);
		this.m_fileParsingRB.Name = "m_fileParsingRB";
		this.m_fileParsingRB.TabIndex = 1;
		this.m_fileParsingRB.TabStop = true;
		this.m_fileParsingRB.Text = "File parsing";
		this.m_fileParsingRB.CheckedChanged += new System.EventHandler(this.m_fileParsingRB_CheckedChanged);
		// 
		// m_fileParsingDirectoryRB
		// 
		this.m_fileParsingDirectoryRB.Location = new System.Drawing.Point(8, 48);
		this.m_fileParsingDirectoryRB.Name = "m_fileParsingDirectoryRB";
		this.m_fileParsingDirectoryRB.Size = new System.Drawing.Size(112, 24);
		this.m_fileParsingDirectoryRB.TabIndex = 0;
		this.m_fileParsingDirectoryRB.Text = "Directory parsing";
		this.m_fileParsingDirectoryRB.CheckedChanged += new System.EventHandler(this.m_fileParsingDirectoryRB_CheckedChanged);
		// 
		// m_proceedBtn
		// 
		this.m_proceedBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
		this.m_proceedBtn.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
		this.m_proceedBtn.Location = new System.Drawing.Point(584, 544);
		this.m_proceedBtn.Name = "m_proceedBtn";
		this.m_proceedBtn.Size = new System.Drawing.Size(72, 23);
		this.m_proceedBtn.TabIndex = 2;
		this.m_proceedBtn.Text = "Proceed";
		this.m_proceedBtn.Click += new System.EventHandler(this.m_proceedBtn_Click);
		// 
		// m_cancelBtn
		// 
		this.m_cancelBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
		this.m_cancelBtn.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
		this.m_cancelBtn.Location = new System.Drawing.Point(8, 544);
		this.m_cancelBtn.Name = "m_cancelBtn";
		this.m_cancelBtn.TabIndex = 1;
		this.m_cancelBtn.Text = "Cancel";
		this.m_cancelBtn.Click += new System.EventHandler(this.m_cancelBtn_Click);
		// 
		// m_encodingGB
		// 
		this.m_encodingGB.Controls.Add(this.m_encodingCombo);
		this.m_encodingGB.Location = new System.Drawing.Point(144, 16);
		this.m_encodingGB.Name = "m_encodingGB";
		this.m_encodingGB.Size = new System.Drawing.Size(208, 88);
		this.m_encodingGB.TabIndex = 7;
		this.m_encodingGB.TabStop = false;
		this.m_encodingGB.Text = "Encoding";
		// 
		// m_encodingCombo
		// 
		this.m_encodingCombo.Location = new System.Drawing.Point(8, 32);
		this.m_encodingCombo.Name = "m_encodingCombo";
		this.m_encodingCombo.Size = new System.Drawing.Size(184, 21);
		this.m_encodingCombo.TabIndex = 4;
		// 
		// m_addFilesFD
		// 
		this.m_addFilesFD.DefaultExt = "htm";
		this.m_addFilesFD.Multiselect = true;
		this.m_addFilesFD.RestoreDirectory = true;
		this.m_addFilesFD.Title = "Add files to parse";
		// 
		// m_mainMenuMN
		// 
		this.m_mainMenuMN.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																																							 this.m_configureMI});
		// 
		// m_configureMI
		// 
		this.m_configureMI.Index = 0;
		this.m_configureMI.MdiList = true;
		this.m_configureMI.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																																								this.m_textFilteringMI});
		this.m_configureMI.Text = "Configure";
		// 
		// m_textFilteringMI
		// 
		this.m_textFilteringMI.Index = 0;
		this.m_textFilteringMI.Text = "Text filtering";
		this.m_textFilteringMI.Click += new System.EventHandler(this.m_textFilteringMI_Click);
		// 
		// MDIView
		// 
		this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
		this.BackColor = System.Drawing.SystemColors.Control;
		this.ClientSize = new System.Drawing.Size(680, 590);
		this.ContextMenu = this.m_contextMenu;
		this.Controls.Add(this.m_fileParsingGB);
		this.Menu = this.m_mainMenuMN;
		this.Name = "MDIView";
		this.Text = "Text parsing";
		this.Closing += new System.ComponentModel.CancelEventHandler(this.ClosingHandler);
		this.Closed += new System.EventHandler(this.ClosedHandler);
		this.Paint += new System.Windows.Forms.PaintEventHandler(this.PaintHandler);
		this.m_fileParsingGB.ResumeLayout(false);
		this.panel1.ResumeLayout(false);
		this.m_outputDirectoryGB.ResumeLayout(false);
		this.m_parsingTypeGB.ResumeLayout(false);
		this.m_encodingGB.ResumeLayout(false);
		this.ResumeLayout(false);

	}

  private void Init()
  {
    m_encodingCombo.DataSource = ENCODING;
    m_encodingCombo.SelectedIndex = 0;
    if (m_fileParsingRB.Checked) m_parsingMode = ParsingMode.FILE;
    else m_parsingMode = ParsingMode.DIRECTORY;

    m_addFilesFD.Filter = "html files (*.htm,*.html)|*.htm;*.html|All files (*.*)|*.*" ;

    m_filesToParseList = new ArrayList();
    m_progress = new Progress();
  }

	private void PaintHandler(Object sender, PaintEventArgs e)
	{
		Rectangle rectClip = e.ClipRectangle;
	}

	public void ClosingHandler(Object sender, CancelEventArgs e)
	{
      myDoc.viewList.Dispose();
			myDoc.viewList = null;
	}

	public void ClosedHandler(Object sender,EventArgs e)
	{		
		//If there are no child views, then disable menu and toolbar items
		if(mainWin.MdiChildren.Length   == 0 ) mainWin.DisableItems();					
	}
	
	public MDIDoc GetDocument()
	{
		return myDoc;
	}

  public IList FilesToParse
  {
    get { return m_filesToParseList; }
  }

  /// <summary>
  /// Encoding to be applied for text parsing
  /// </summary>
  public System.Text.Encoding ParsingEncoding
  {
    get 
    {
      Enc encoding = (Enc) m_encodingCombo.SelectedItem;
      return encoding.Encoding;
    }
  }

  /// <summary>
  /// The path were parsed files will be saved
  /// </summary>
  public string OutputPath
  {
    get { return m_outputDirectoryTB.Text; }
  }

  private void m_proceedBtn_Click(object sender, System.EventArgs e)
  {
    string outputpath = m_outputDirectoryTB.Text;
    if (outputpath == String.Empty)
    {
      MessageBox.Show(this,"Provide valid path to save parsed files","Directory does not exist",MessageBoxButtons.OK,MessageBoxIcon.Error);
      return;
    }
    if (!Directory.Exists(outputpath))
    {
      MessageBox.Show(this,"\"" + outputpath + "\" directory does not exist","Directory does not exist",MessageBoxButtons.OK,MessageBoxIcon.Error);
      return;
    }
    if (m_filesToParseList.Count <= 0)
    {
      MessageBox.Show(this,"The file list is empty\nChoose at least one file to parse","No files chosen",MessageBoxButtons.OK,MessageBoxIcon.Error);
      return;
    }
    m_proceedBtn.Enabled = false;

    char lastChar = outputpath[outputpath.Length - 1];
    if (!(lastChar.Equals('\\') && lastChar.Equals('/'))) outputpath += '\\';

    m_progress.MaximumValue = m_filesToParseList.Count;
    m_progress.CurrentValue = 0;
    m_progress.Show();

    myDoc.Encoding = this.ParsingEncoding;
    myDoc.OutputPath = this.OutputPath;
    myDoc.FilesToParse = this.FilesToParse;


    ThreadStart threadStart = new ThreadStart(myDoc.ProceedParsing);
    Thread thread = new Thread(threadStart);
    thread.Start();

    m_proceedBtn.Enabled = true;

  }

  private void m_cancelBtn_Click(object sender, System.EventArgs e)
  {
    this.Dispose();
    myDoc.viewList = null;
  }

  private void m_outputDirectoryBtn_Click(object sender, System.EventArgs e)
  {
    try
    {

			FolderBrowserDialog dialog = new FolderBrowserDialog();
			DialogResult result = dialog.ShowDialog();
			if(result==DialogResult.OK)
      {
        string directoryPath = dialog.SelectedPath;
        char lastChar = directoryPath[directoryPath.Length - 1];
        if (!(lastChar.Equals('\\') && lastChar.Equals('/'))) directoryPath += '\\';
        m_outputDirectoryTB.Text = directoryPath;
      }
    }
    catch(Exception ex)
    {
      MessageBox.Show(this,ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
  }

  private void m_addContext_Popup(object sender, System.EventArgs e)
  {
    
    if (m_parsingMode == ParsingMode.FILE) m_addContext.Text = "Add files";
    else m_addContext.Text = "Add directory";
  
  }

  private void m_fileParsingDirectoryRB_CheckedChanged(object sender, System.EventArgs e)
  {
    if (m_fileParsingDirectoryRB.Checked) m_parsingMode = ParsingMode.DIRECTORY;
  }

  private void m_fileParsingRB_CheckedChanged(object sender, System.EventArgs e)
  {
     if (m_fileParsingRB.Checked) m_parsingMode = ParsingMode.FILE;
  }	

  private void m_contextMenu_Popup(object sender, System.EventArgs e)
  {
    if (m_parsingMode == ParsingMode.FILE) 
    {
      m_addContext.Text = "Add files";
      m_deleteContext.Text = "Delete file";
    }
    else 
    {
      m_addContext.Text = "Add directory";
      m_deleteContext.Text = "Delete file";
    }
  }

  private void m_clearAllContext_Click(object sender, System.EventArgs e)
  {
    m_fileParsingLB.DataSource = null;
    m_filesToParseList.Clear();
    m_fileParsingLB.DataSource = m_filesToParseList;
  }

  public string AddDirectory()
  {
		string directory = null;
    
    try
    {
      FolderBrowserDialog dialog = new FolderBrowserDialog();
      DialogResult result = dialog.ShowDialog();
      if (result == DialogResult.OK)
      {
        string directoryPath = dialog.SelectedPath;
        char lastChar = directoryPath[directoryPath.Length - 1];
        if (!(lastChar.Equals('\\') && lastChar.Equals('/'))) directoryPath += '\\';
        directory = directoryPath;
      }
    }
    catch(Exception ex)
    {
      MessageBox.Show(this,ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
    return directory;
  }

  /// <summary>
  /// Add files to list of files to parse
  /// </summary>
  private void AddContext()
  {
    if (m_parsingMode == ParsingMode.FILE)
    {
      AddFiles();
    }
    else
    {
      string directory = AddDirectory();
      RetrieveFilesFromDirectory(directory);
    }
  }

  /// <summary>
  /// Add files to the list of files to parse
  /// </summary>
  /// <returns></returns>
  private void AddFiles()
  {
    string [] files = null;
    try
    {
      if (m_addFilesFD.ShowDialog() == DialogResult.OK)
      {
        files = m_addFilesFD.FileNames; 
        AddNewFilesToList(files);
      }
    }
    catch(Exception ex)
    {
      MessageBox.Show(this,ex.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
    }
  }

  private void DeleteSelectedFiles()
  {
    ListBox.SelectedIndexCollection indices = m_fileParsingLB.SelectedIndices;

    if (indices.Count <= 0) return;

    for (int i=indices.Count - 1;i>=0;i--)
    {
      m_filesToParseList.RemoveAt(indices[i]);
    }
    m_fileParsingLB.DataSource = null;
    m_fileParsingLB.DataSource = m_filesToParseList;
  }

  private void RetrieveFilesFromDirectory(string directory)
  {
    if (directory == null) return;
    string [] files = null;
    files = Directory.GetFiles(directory,"*.htm");
    if (files != null) AddNewFilesToList(files);
  }

  /// <summary>
  /// Check for duplicate files and add only unique
  /// </summary>
  /// <param name="files"></param>
  private void AddNewFilesToList(string [] files)
  {
    m_fileParsingLB.BeginUpdate();
    for (int i=0;i<files.Length;i++)
    {
      if (!m_filesToParseList.Contains(files[i])) m_filesToParseList.Add(files[i]);
    }
    m_fileParsingLB.DataSource = null;
    m_fileParsingLB.EndUpdate();
    m_fileParsingLB.DataSource = m_filesToParseList;
    
  }

  private void m_addContext_Click(object sender, System.EventArgs e)
  {
    AddContext();
  }

  private void m_deleteContext_Click(object sender, System.EventArgs e)
  {
    DeleteSelectedFiles();
  }


  public void ReportProgressEventHandler(object sender,Slaks.ParserUtil.ParseEventArgs args)
  {
     m_progress.CurrentValue += 1;
    m_messagesLB.BeginUpdate();
     m_messagesLB.Items.Add(args.Message);
    m_messagesLB.EndUpdate();
  }

  private void menuItem1_Click(object sender, System.EventArgs e)
  {
    MessageBox.Show(this,m_filesToParseList.Count + " files in the list","File counter");
  }

  private void m_textFilteringMI_Click(object sender, System.EventArgs e)
  {
    if (m_textFiltering == null) m_textFiltering = new TextFiltering();

    m_textFiltering.ShowDialog();

    if (m_textFiltering.ISUseReaderAndFilterModules == true)
    {
      myDoc.BufferReader = m_textFiltering.BufferReader;
      myDoc.Filter = m_textFiltering.Filter;

			MDIDoc.ISUseStopwordCollectionModule = m_textFiltering.ISUseStopwordCollectionModule;
			if (MDIDoc.ISUseStopwordCollectionModule)
			{
				myDoc.StopwordCollectionFileReader = m_textFiltering.StopwordCollectionFileReader;
				myDoc.StopwordCollectionFileName = m_textFiltering.StopwordCollectionFileName;
				myDoc.StopwordCollectionFilter = m_textFiltering.StopwordCollectionFilter;
			}

    }

    myDoc.ISUseReaderFilterModules = m_textFiltering.ISUseReaderAndFilterModules;
}

}
}
